<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace app\admin\controller;

use app\admin\model\Subject;
use app\admin\model\Grade;
use think\Db;

class DashboardController extends CommonController {

    /**
     * 获取轮播图信息
     */
    public function getData() {
        $rows = $_POST['rows'];
        $page = $_POST['page'];
        $param = $this->getDataByCampusid($_POST);
        $notice = Db::table('ew_index')
            ->where($param)
            ->limit($rows * ($page - 1), $rows)
            ->select();
        $total = Db::table('ew_index')
            ->where($param)
            ->count();
        $data['total'] = $total;
        $data['rows'] = $notice;
        return json_encode($data);
    }

    /**
     * addImg 添加图片页面
     * @return mixed
     */
    public function addImg(){
        return $this->fetch('ajax/addimg');
    }

    public function upload(){
        $file = request()->file('file');
        $dir = ROOT_PATH . 'static' . DS . 'uploads/images';
        if(!is_dir($dir)){
            if(!mkdir($dir,0777,true)){
                return false;
            }
        }
        $info = $file->move(ROOT_PATH . 'static' . DS . 'uploads/images');
        $new_name = $info->getSaveName();
        image_png_size_add(ROOT_PATH . 'static' . DS . 'uploads/images/'.$new_name,ROOT_PATH . 'static' . DS . 'uploads/images/'.$new_name);
        echo $new_name;
//       $file = request()->file('file');
//       $info = $file->move(ROOT_PATH . 'static' . DS . 'uploads/images');
//       if($info){
//            echo $info->getSaveName();
//        }else{
//            echo $file->getError();
//        }
    }

    public function deleteImg(){
        $add = input('add');
        if(unlink($add)){
            return json(['code'=>200,'msg'=>'删除成功']);
        }else{
            return json(['code'=>100,'msg'=>'删除失败']);
        }
    }

    public function insertImg(){
        $photo = input('photo');
        $photo = trim($photo,',');
        $res = Db::table('ew_index')
            ->insert(['image'=>$photo,'create_time'=>date("Y-m-d H:i:s")]);
        if($res){
            return json(['code'=>200,'msg'=>'保存成功']);
        }else{
            return json(['code'=>100,'msg'=>'保存失败']);
        }
    }

    public function delImg(){
        $res = Db::table('ew_index')->delete(input('id'));
        if($res){
            return json(['code'=>200,'msg'=>"删除成功"]);
        }else{
            return json(['code'=>100,'msg'=>"删除失败"]);
        }
    }

    public function updateImg(){
        $data = Db::table('ew_index')->where('id',input('id'))->value('image');
        $images = trim($data,',');
        $data = explode(',',$images);
        $this->assign ('id',input('id'));
        $this->assign ('images',$images);
        $this->assign ('photo',$data);
        return $this->fetch('ajax/editImg');
    }

    /**
     * imgDelete 删除图片
     * @return \think\response\Json
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function imgDelete(){
        extract(input());
        $photo = Db::table('ew_index')->where('id',$id)->value('image');
        $add = str_replace('static/uploads/images/',"",$add);
        $imgs = explode(',',$photo);;
        if(in_array($add,$imgs)){
            $key = array_search ($add,$imgs);
            $place  = 'static/uploads/images/'.$imgs[$key];
            unset($imgs[$key]);
            $img = implode(',',$imgs);
            trim($img,',');
            $res = Db::table('ew_index')->where('id',$id)->update(['image'=>$img]);
            if($res){
                unlink ($place);
                return json(['code'=>200,'msg'=>'删除成功']);
            }else{
                return json(['code'=>100,'msg'=>'删除失败！']);
            }
        }
    }

    /**
     * updatePhoto 修改图片
     * @return \think\response\Json
     */
    public function updatePhoto(){
        extract(input());
        $photo = Db::table('ew_index')->where('id',$id)->value('image');
        $pho = $photo.",".$add;
        $res = Db::table('ew_index')->where('id',$id)->update(['image'=>$pho]);
        if($res){
            return json(['code'=>200,'msg'=>'']);
        }else{
            return json(['code'=>100,'msg'=>'']);
        }
    }

    public function editImg(){
        extract(input());
        $photo = trim($photo,',');
        $res = Db::table('ew_index')->where('id',$id)->update (['image'=>$photo,'update_time'=>date('Y-m-d H:i:s')]);
        if($res){
            return json(['code'=>200,'msg'=>'修改成功']);
        }else{
            return json(['code'=>100,'msg'=>'修改失败']);
        }
    }
}

